package com.example.filmsearchapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.appyvet.materialrangebar.RangeBar;
import com.example.filmsearchapplication.model.Film;
import com.example.filmsearchapplication.utils.DataManager;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "<Test>";
    private static Context context;

    private ArrayList<Film> filmList;
    private ArrayList<Film> filmResultList;
    private int resultIndex;

    public class SearchInfo{
        private String director;
        private String name;
        private int yearStart;
        private int yearEnd;
        private int hasSubtitle;
        private boolean isTypeScience;
        private boolean isTypeLove;
        private boolean isTypeBiography;
        private boolean isTypeSuspense;
        private boolean isTypeComedy;
    }
    private SearchInfo searchInfo;
    private EditText mEtDirector,mEtName,mEtYearStart, mEtYearEnd;
    private RadioGroup mRgSubtitle;
    private RadioButton mRbSubtitleYes,mRbSubtitleNo,mRbSubtitleWhatever;
    private CheckBox mCbScience,mCbLove,mCbBiography,mCbSuspense,mCbComedy;
    private TextView mTvResultNum,mTvName,mTvDirector,mTvType,mTvYear,mTvSubtitle;
    private SeekBar mSbYear;
    private Button mBtnSearch,mBtnNext;
    private ImageView mIvPlayBill;
    private RangeBar mRbYear;

    public static Context getContext(){
        return context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        //初始化数据
        initData();
        //初始化控件
        initView();
        //设置监听器
        setListeners();
    }

    private void initData() {
        filmList = DataManager.getDefaultDataManager().getFilmList();
    }

    private void initView() {
        mEtDirector = findViewById(R.id.target_director);
        mEtName = findViewById(R.id.target_name);
        mRgSubtitle = findViewById(R.id.target_subtitle);
        mRbSubtitleYes = findViewById(R.id.target_subtitle_yes);
        mRbSubtitleNo = findViewById(R.id.target_subtitle_no);
        mRbSubtitleWhatever = findViewById(R.id.target_subtitle_whatever);
        mRgSubtitle.check(mRbSubtitleWhatever.getId());
        mCbScience = findViewById(R.id.target_type_scienceFiction);
        mCbComedy = findViewById(R.id.target_type_comedy);
        mCbBiography = findViewById(R.id.target_type_biography);
        mCbLove = findViewById(R.id.target_type_love);
        mCbSuspense = findViewById(R.id.target_type_suspense);
        mEtYearStart = findViewById(R.id.target_year_start);
        mEtYearEnd = findViewById(R.id.target_year_end);
        mTvResultNum = findViewById(R.id.resultNum);
        mTvName = findViewById(R.id.tv_name);
        mTvDirector = findViewById(R.id.tv_director);
        mTvType = findViewById(R.id.tv_types);
        mTvYear = findViewById(R.id.tv_year);
        mTvSubtitle = findViewById(R.id.tv_subtitle);
        mBtnSearch = findViewById(R.id.search);
        mBtnNext = findViewById(R.id.next);
        mIvPlayBill = findViewById(R.id.playBill);
        mRbYear = findViewById(R.id.rangebar_year);
    }



    private void setListeners() {
        mEtYearStart.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d(TAG,s.toString());
            }
        });
        mRbYear.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {

            }

            @Override
            public void onTouchStarted(RangeBar rangeBar) {

            }

            @Override
            public void onTouchEnded(RangeBar rangeBar) {
                mEtYearStart.setText(new Integer(rangeBar.getLeftPinValue()).toString());
                mEtYearEnd.setText(new Integer(rangeBar.getRightPinValue()).toString());
            }
        });

        mBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
                updateView();
            }
        });

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateView();
            }
        });
    }



    private void search() {
        if(searchInfo==null){
            searchInfo = new SearchInfo();
        }
        if(filmResultList==null){
            filmResultList = new ArrayList<Film>();
        }
        filmResultList.clear();
        resultIndex = 0;

        updateSearchInfo();

        for(Film film:filmList){
            //查询导演名称
            if((!searchInfo.director.equals(film.getDirector()))&&!searchInfo.director.equals(""))
                continue;
            //查询影片名称
            if((!searchInfo.name.equals(film.getName()))&&(!searchInfo.name.equals("")))
                continue;
            //查询年份
            if(!(film.getYear()>=searchInfo.yearStart&&film.getYear()<= searchInfo.yearEnd))
                continue;
            //查询字幕
            if(searchInfo.hasSubtitle!=0){
                if((film.isSubtitle()^(searchInfo.hasSubtitle==1)))//相同为0，不同为1，
                    continue;
            }
            //查询类型，规则为选择多项条件更广,如果一个都没选则全部添加
            if((!searchInfo.isTypeScience)&&(!searchInfo.isTypeSuspense)&&(!searchInfo.isTypeLove)&&(!searchInfo.isTypeBiography)&&(!searchInfo.isTypeComedy)){
                filmResultList.add(film);
                continue;
            }
            for(String type :film.getTypes().split("-")){
                if(searchInfo.isTypeComedy&&type.equals("喜剧")
                    ||searchInfo.isTypeLove&&type.equals("爱情")
                    ||searchInfo.isTypeSuspense&&type.equals("悬疑")
                    ||searchInfo.isTypeBiography&&type.equals("传记")
                    ||searchInfo.isTypeScience&&type.equals("科幻")){
                    filmResultList.add(film);
                    break;
                }
            }
        }
        mTvResultNum.setText("符合条件的结果有"+String.valueOf(filmResultList.size())+"个");
    }

    private void updateView() {
        if(resultIndex==filmResultList.size()){
            resultIndex=0;
        }

        mIvPlayBill.setImageResource(filmResultList.get(resultIndex).getPlayBillId());
        mTvName.setText(filmResultList.get(resultIndex).getName());
        mTvDirector.setText(filmResultList.get(resultIndex).getDirector());
        mTvType.setText(filmResultList.get(resultIndex).getTypes());
        mTvYear.setText(String.valueOf(filmResultList.get(resultIndex).getYear()));
        mTvSubtitle.setText(filmResultList.get(resultIndex).isSubtitle()?"中英字幕":"英文字幕");

        resultIndex++;

    }
    private void updateSearchInfo() {
        searchInfo.director = mEtDirector.getText().toString();
        searchInfo.name = mEtName.getText().toString();
        searchInfo.yearStart = Integer.parseInt(mEtYearStart.getText().toString());
        searchInfo.yearEnd = Integer.parseInt(mEtYearEnd.getText().toString());
        searchInfo.hasSubtitle = mRbSubtitleYes.isChecked()?1:(mRbSubtitleNo.isChecked()?-1:0);
        searchInfo.isTypeScience = mCbScience.isChecked();
        searchInfo.isTypeLove = mCbLove.isChecked();
        searchInfo.isTypeBiography = mCbBiography.isChecked();
        searchInfo.isTypeComedy = mCbComedy.isChecked();
        searchInfo.isTypeSuspense = mCbSuspense.isChecked();
    }
}